package com.otod.pm.comment.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.otod.base.exception.AppException;
import com.otod.base.service.BaseService;
import com.otod.commons.utils.BeanUtils;
import com.otod.commons.utils.Collections3;
import com.otod.commons.utils.StringUtils;
import com.otod.pm.comment.bo.AsmgCommentBo;
import com.otod.pm.comment.mapper.AsmgCommentMapper;
import com.otod.pm.comment.vo.AsmgCommentVo;
import com.otod.pm.doc.mapper.PmDocTpItemMapper;

@Service
public class CommentService extends BaseService{
	protected static final Logger logger = LoggerFactory.getLogger(CommentService.class);
	
	@Autowired  
	private PmDocTpItemMapper docTpMapper;
	
	
	@Autowired  
	private AsmgCommentMapper commentMapper;
	
	
	
	
	
	
	
	/**
	 * 查询评论列表
	 */
	public List<AsmgCommentVo> queryForList(Map<String, Object> params){
		return commentMapper.queryForList(params);
	}
	
	/**
	 * 查询comment
	 * param id
	 */
	public AsmgCommentVo getById(String id){
		return commentMapper.getById(id);
	}
	
	
	
	/**
	 * 删除
	 * @throws AppException 
	 */
	@Transactional
	public void deleteById(String commentId) throws AppException{
		commentMapper.deleteById(commentId);
	}
	
	
	
	
	
	/**
	 * 新增
	 */
	@Transactional
	public void insert(AsmgCommentVo commentVo){
		if(!StringUtils.isEmpty(commentVo.getParentId())){
			AsmgCommentVo parentCommentVo = this.getById(commentVo.getParentId());
			commentVo.setReplyToEmpNo(parentCommentVo.getCreateEmpNo());
			commentVo.setReplyToEmp(parentCommentVo.getCreateEmp());
		};
		commentVo.setUuid(super.getUUID());
		commentMapper.insert(commentVo);
	}
	
	/**
	 * 修改
	 */
	@Transactional
	public void update(Map map){
		docTpMapper.update(map);
	}
	
	
	
	/**
	 * 获取当前评论的相关评论
	 * @param parentId 父级评论ID
	 * @return
	 */
	public List<AsmgCommentBo> queryChildrenForList(String busitableKey){
		List<AsmgCommentBo> commentList = new ArrayList<AsmgCommentBo>();
		List<AsmgCommentBo> childrenList;
		AsmgCommentBo commentBo;
		
		if(StringUtils.isEmpty(busitableKey)){
			return commentList;
		}
		
		Map<String,Object> params = new HashMap<String, Object>();
		params.put("busitableKey", busitableKey);
		
		List<AsmgCommentVo> allList = commentMapper.queryForList(params);
		if(Collections3.isNotEmpty(allList)){
			for (AsmgCommentVo asmgCommentVo : allList) {
				if(!StringUtils.isEmpty(asmgCommentVo.getParentId())){		//只查找顶级评论
					continue;
				}
				
				commentBo = new AsmgCommentBo();
				BeanUtils.copyProperties(asmgCommentVo, commentBo);
				
				childrenList = new ArrayList<AsmgCommentBo>();
				getChildrenMenu(asmgCommentVo.getUuid(), childrenList, allList);
				commentBo.setChildList(childrenList);
				
				commentList.add(commentBo);
				
			}
		}
		return commentList;
		
	}
	
	/**
	 * 获取子评论
	 * @param topId
	 * @param childrenList
	 * @param allList
	 */
	private void getChildrenMenu(String topId,List<AsmgCommentBo> childrenList,List<AsmgCommentVo> allList){
		AsmgCommentBo commentBo;
		
		for (AsmgCommentVo commentVo : allList) {
			if(topId.equals(commentVo.getTopId())){
				commentBo = new AsmgCommentBo();
				BeanUtils.copyProperties(commentVo, commentBo);
				
				childrenList.add(commentBo);
			}
		}
	}
}
